package com.service.component.common;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import lombok.*;

import java.io.Serializable;


@Data
@ApiModel(value = "CommonResult", description = "统一返回实体类")
/**
 * @author ：Calvin
 * @date ：Created in 2021/7/16 14:35
 * @description：全局统一返回实体类
 * @modified By：
 * @version: 1.0.0$
 */
public class CommonResult<T> implements Serializable {

    /**
     * 请求状态
     */
    private boolean status;

    /**
     * 异常码
     */
    private Integer code;

    /**
     * 异常提示
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;


    @ToString
    @Getter
    @ApiModel(value = "ResultCode", description = "全局返回code以及信息")
    public enum ResultCode {
        SUCCESS(true, 200, "请求成功"),
        TONKEN_FAILURE(false, 401, "tonken失效"),
        NOT_FOUND(false, 404, "没有找到请求"),
        SERVER_ERROR(false, 500, "服务器异常"),
        USERNAME_OR_PASSWORD_ERROR(false, 10000, "用户名或密码异常"),
        NOT_PERMISSIONS_ERROR(false, 10001, "权限不足"),
        DATA_EXIST(false, 10002, "数据已存在"),
        DATA_NOT_EXIST(false, 10003, "数据不存在"),
        INVALID_PARAMERS_ERROR(false, 10004, "参数异常"),
        REQUEST_DATE_ERROR(false,10005,"请求过时"),
        NOT_MISSING_PARAMERS_ERROR(false, 10006, "参数缺失"),
        OUT_OF_RANGE_ERROR(false,10007,"范围超出边界"),
        INVALID_FORMAT_ERROR(false,10009,"数据格式异常"),
        REQUEST_SQL_ERROR(false,10010,"请求数据库异常"),
        CHECK_ERROR(false,10011,"校验失败"),
        UNAUTHENTICATED_ERROR(false, 10012, "没有登录认证"),
        UNAUTHENTICATED_OR_NOT_FOUND_ERROR(false, 10013, "没有找到请求或没有登录"),
        NOT_ROLES_ERROR(false, 10014, "没有相关角色"),
        SQL_ERROR(false, 10015, "SQL语句异常");

        private final boolean status;
        private final int code;
        private final String message;

        ResultCode(boolean status, int code, String message) {
            this.status = status;
            this.code = code;
            this.message = message;

        }
    }

    private CommonResult() {}

    public CommonResult(ResultCode resultCode) {
        this.status = resultCode.status;
        this.code = resultCode.code;
        this.message = resultCode.message;
    }

    public CommonResult(ResultCode resultCode, T data) {
        this.status = resultCode.status;
        this.code = resultCode.code;
        this.message = resultCode.message;
        this.data = data;
    }

    public CommonResult(boolean status, Integer code, String message) {
        this.status = status;
        this.code = code;
        this.message = message;
    }

    public CommonResult(boolean status, Integer code, String message, T data) {
        this.status = status;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static CommonResult success() {
        return new CommonResult(ResultCode.SUCCESS);
    }

    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<T>(ResultCode.SUCCESS, data);
    }

    public static CommonResult error() {
        return new CommonResult(ResultCode.SERVER_ERROR);
    }

    public static CommonResult commonResult(ResultCode code) {
        return new CommonResult(code);
    }

    public static <T> CommonResult<T> commonResult(boolean success, Integer code, String message, T data) {
        return new CommonResult<T>(success, code, message, data);
    }
}